/***********************************************************************
	filename: 	DaterRecEvent.h
	created:	22/4/2012
	author:		4Ys
	
	purpose:	Implements classes to recover orders from server to client,
				specifically for event.	            
*************************************************************************/

#include "DaterRecEvent.h"
#include "DaterEventManager.h"
#include "DaterCommon.h"
#include "DaterGroupEvent.h"
#include "DaterSystem.h"

#include<iostream>

namespace Dater
{
	//Add
	bool RecEventAdd::execute()
	{
		std::string object, operation, isSuccessful;
		if(_splitedData[0] == "0" && _splitedData[1] == "0")
		{
			object = _splitedData[0];
			operation = _splitedData[1];
			isSuccessful = _splitedData[2];
			if(isSuccessful == "1")
				return true;
			else
				return false;
		}
		else
			printf("Error\n");
		return false;
	}

	//Update
	bool RecEventUpdate::execute()
	{
		std::string object, operation, isSuccessful;
		if(_splitedData[0] == "0" && _splitedData[1] == "1")
		{
			object = _splitedData[0];
			operation = _splitedData[1];
			isSuccessful = _splitedData[2];
			if(isSuccessful == "1")
				return true;
			else
				return false;
		}
		else
			printf("Error\n");
		return false;
	}

	//Delete
	bool RecEventDelete::execute()
	{
		std::string object, operation, isSuccessful;
		if(_splitedData[0] == "0" && _splitedData[1] == "2")
		{
			object = _splitedData[0];
			operation = _splitedData[1];
			isSuccessful = _splitedData[2];
			if(isSuccessful == "1")
				return true;
			else
				return false;
		}
		else
			printf("Error\n");
		return false;
	}

	//View
	Event* RecEventView::execute()
	{
		//string: eventID@userID@name@createTime@startTime@finishTime@description
		std::string type, name, description;
		unsigned int userID, eventID;
		Time createTime, startTime, finishTime;

		/*
		//check:  
		std::cout << "index:  " << i << "\n";
		std::cout << "_eventData: " << _eventData << "\n";
		for (int j = 0; j != _dataVector.size()-1; j++)
			std::cout << _dataVector[j] << "  ";
		std::cout << "\n-------------\n";
		*/
	//initialize local variables for a certain Event.
		eventID = atoi(_splitedData[3].c_str());
		userID = atoi(_splitedData[2].c_str());
		name = _splitedData[4];
		createTime = stringToTime(_splitedData[5]);
		startTime = stringToTime(_splitedData[6]);
		finishTime = stringToTime(_splitedData[7]);
		description = _splitedData[8];
		type = _splitedData[9];
			
		Event* tempEvent = EventManager::getSingleton().addEvent(type, name, startTime, finishTime, createTime, description, userID, eventID);
		
		Dater::System::getSingleton().getCalendarData()->addEvent(tempEvent);

		int baseNum;
		if(type == "GroupEvent")
		{
			static_cast<GroupEvent*>(tempEvent)->setGroupID(atoi(_splitedData[10].c_str()));
			static_cast<GroupEvent*>(tempEvent)->setGroupEventID(atoi(_splitedData[11].c_str()));
			baseNum = 11;
		}else
		{
			baseNum = 9;
		}

		if(_splitedData[baseNum + 1] == "1")
		{
			int remindTime;
			std::string remindUnit;
			remindTime = atoi(_splitedData[baseNum + 2].c_str());
			remindUnit = _splitedData[baseNum + 3];
			tempEvent->setReminding(remindTime, remindUnit);

			if(_splitedData[baseNum + 4] == "1")
			{
				int repeatInterval;
				std::string repeatUnit;
				Time repeatEndTime;
				repeatInterval = atoi(_splitedData[baseNum + 5].c_str());
				repeatUnit = _splitedData[baseNum + 6];
				repeatEndTime = stringToTime(_splitedData[baseNum + 7]);
				tempEvent->setRepeating(repeatInterval, repeatUnit, repeatEndTime);
			}
		}else
		{
			if(_splitedData[baseNum + 2] == "1")
			{
				int repeatInterval;
				std::string repeatUnit;
				Time repeatEndTime;
				repeatInterval = atoi(_splitedData[baseNum + 3].c_str());
				repeatUnit = _splitedData[baseNum + 4];
				repeatEndTime = stringToTime(_splitedData[baseNum + 5]);
				tempEvent->setRepeating(repeatInterval, repeatUnit, repeatEndTime);
			}
		}
		return tempEvent;
	}

	/*std::vector<Event*> RecEventView::getEvent()
	{
		return _resultEvent;
	}*/
}